using UnityEngine;
using UnityEngine.Rendering;
using Yoozoo.Core.Extensions.CSharp;

namespace Yoozoo.Gameplay.Liberty
{
    public class LibertyWeapon : MonoBehaviour
    {
        // 子弹发射挂点
        public Transform FirePoint;
        // 开火特效挂点
        public Transform MuzzleFlashPoint;
        // 子弹弹夹挂点
        public Transform ClipPoint;
        // 子弹弹壳弹出挂点
        public Transform ShellEjectPoint;
        // 射击之后，需要隐藏的物体
        public Transform FireHideObject;
        
        public MeshRenderer[] Renderers;

        /// <summary>
        /// 开火回调
        /// </summary>
        public void OnWeaponFire()
        {
            if (FireHideObject != null)
            {
                FireHideObject.gameObject.SetActive(false);
            }
        }

        /// <summary>
        /// 装弹回调
        /// </summary>
        public void OnWeaponReload()
        {
            if(FireHideObject != null)
            {
                FireHideObject.gameObject.SetActive(true);
            }
        }

        private int _lod = -1;
        public void ChangeLod(int lod)
        {
            if (_lod == lod)
                return;

            _lod = lod;
            if (lod == 0)
            {
                foreach (var meshRenderer in Renderers)
                {
                    meshRenderer.shadowCastingMode = ShadowCastingMode.On;
                    meshRenderer.enabled = true;
                }
            }
            else if (lod == 1)
            {
                foreach (var meshRenderer in Renderers)
                {
                    meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
                    meshRenderer.enabled = true;
                }
            }
            else
            {
                foreach (var meshRenderer in Renderers)
                {
                    meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
                    meshRenderer.enabled = false;
                }
            }
        }

#if UNITY_EDITOR
        void OnValidate()
        {
            if (Renderers.IsEmpty())
            {
                Renderers = gameObject.GetComponentsInChildren<MeshRenderer>(true);
            }
        }
#endif
    }
}
